package com.phynos.lib.common.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;

public class ExcelUtils {

	public static void toExcel(Workbook wb, HttpServletResponse response,
			String name) throws Exception {
		response.setContentType("application/vnd.ms-excel;charset=UTF8");
		response.setHeader("Content-disposition", "attachment;filename="
				+ new String(name.getBytes("gb2312"), "iso-8859-1") + ".xls");
		ServletOutputStream os = response.getOutputStream();
		wb.write(os);
		os.flush();
	}
	
	public static void showDownLoadError(HttpServletResponse response,String msg) throws Exception {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out = null;
		try {
		    out = response.getWriter();
		    out.write(msg);
		} catch (IOException e) {
		    e.printStackTrace();
		} finally {
		    if (out != null) {
		        out.close();
		    }
		}
	}
	
}
